/**
 * Created by aaa on 2017/5/17.
 */
angular.module('myApp')

    .controller('queryCtrl', ['$scope', '$http', '$httpParamSerializerJQLike', '$log', 'OpenService', 'CheckboxService',
        'CacheService', 'limitToFilter', 'filterFilter', 'dateFilter', 'myConstant', 'DateCalculateService', function ($scope, $http, $httpParamSerializerJQLike, $log, OpenService, CheckboxService,
                                                                                                                       CacheService, limitToFilter, filterFilter, dateFilter, myConstant, DateCalculateService) {
            //视图层变量viewModel
            var vm = $scope.vm = {};
            vm.constant = myConstant;
            //变量初始化
            //分页数据
            vm.pagination = {
                pageSize: 10,
                pageNum: 1
            };
            //查询条件
            vm.queryBean = {
                beginDate: DateCalculateService.getYesterday(),
                endDate: DateCalculateService.getToday()
            };

            //缓存数据初始化(需要缓存的key请自定义)
            /*数据格式{
             key1 :value1,
             key2:value2
             }*/
            vm.cached = {
                SUB_BANK_TYPE: {},
                MERCHANT_CODE: {},
                CONN_CHANNEL: {},
                COMANY_CODE: {}
            };

            CacheService.initCache(vm.cached);

            /*将前面缓存的数据格式化
             [{key:value},{key:value}]*/
            vm.getCache = function (key) {
                return CacheService.getCache(key);
            };

            //获取对象数据[user,user,user]
            vm.getObj = function (key, input) {
                return $http.post(config.ctx + '/base/cache/getAll',
                    $httpParamSerializerJQLike({cacheKey: key}))
                    .then(function (response) {
                        return limitToFilter(filterFilter(response.data.object, input), 6);
                    }, function (response) {
                        $log.error('获取数据%s失败', cacheKey);
                    })
            };

            vm.queryDetail = queryDetail;
            vm.resetForm = resetForm;

            /*------------------以上配置通用---------------------*/


            // vm.updateChecked = updateChecked;
            vm.addItem = addItem;
            vm.updateItem = updateItem;
            // vm.deleteItem = deleteItem;
            // vm.roleAllocate = roleAllocate;

            vm.audit = audit;
            vm.discard = discard;

            /*------------------以上方法名可选择性通用---------------------*/

            /*CacheService.initCache(vm.cached, function (cacheKey, cacheObj) {
             $log.debug(cacheKey, cacheObj);
             vm.cached[cacheKey] = cacheObj;
             });*/

            /*vm.getCache = function (key) {
             CacheService.getCache(key)
             }*/

            function queryDetail() {
                var queryForm = document.getElementById('queryForm');
                vm.url = config.ctx + '/payment/outlineOrder/search';

                var postBean = angular.copy(vm.pagination);
                delete postBean.list;
                postBean.queryBean = vm.queryBean;
                $http.post(vm.url, postBean, config.jsonHeader)
                    .then(function (response) {
                        var data = response.data;
                        if (data.success) {
                            vm.pagination = data.object;
                        } else {
                            alert(data.msg);
                        }
                    });

            }

            function resetForm() {
                vm.queryBean = {};
            }

            function addItem() {
                OpenService({
                    modalTitle: '新增反录',
                    modalBody: 'toAdd',
                    url: 'add',
                    item: {},
                    cached: {
                        MERCHANT_CODE: vm.getCache("MERCHANT_CODE"),
                        SUB_BANK_TYPE: vm.cached.SUB_BANK_TYPE,
                        CONN_CHANNEL: vm.cached.CONN_CHANNEL,
                        COMANY_CODE: vm.getCache("COMANY_CODE")
                    }
                }, function (item) {
                    queryDetail();
                }, 'addItemModalInstanceCtrl', 'myModalNoFooter.html');
            }

            function updateItem() {
                if (CheckboxService.getCheckedNew(vm.pagination.list).length != 1) {
                    alert("必须勾选一条记录！");
                    return;
                }

                var item = CheckboxService.getCheckedNew(vm.pagination.list)[0];
                item = angular.copy(item);
                item.orderAmt = item.orderAmt / 100;

                if (item.status != '01') {
                    alert("已审核，不允许修改!");
                    return;
                }

                OpenService({
                    modalTitle: '修改反录',
                    modalBody: 'toAdd',
                    url: 'update',
                    item: item,
                    cached: {
                        MERCHANT_CODE: vm.getCache("MERCHANT_CODE"),
                        SUB_BANK_TYPE: vm.cached.SUB_BANK_TYPE,
                        CONN_CHANNEL: vm.cached.CONN_CHANNEL,
                        COMANY_CODE: vm.getCache("COMANY_CODE")
                    }
                }, function (item) {
                    queryDetail();
                }, 'addItemModalInstanceCtrl', 'myModalNoFooter.html');
            }

            function audit(id) {

                if (!confirm("确认？")) {
                    return;
                }

                $http.post(config.ctx + '/payment/outlineOrder/audit', {'id': id}, config.jsonHeader)
                    .then(function (response) {
                        var data = response.data;
                        if (data.success) {
                            alert('操作成功');
                            vm.queryDetail();
                        } else {
                            alert(data.msg);
                        }
                    });
            }

            function discard(id) {
                $http.post(config.ctx + '/payment/outlineOrder/discard', {'id': id}, config.jsonHeader)
                    .then(function (response) {
                        var data = response.data;
                        if (data.success) {
                            alert('操作成功')
                            vm.queryDetail();
                        } else {
                            alert(data.msg);
                        }
                    });
            }
        }]);

angular.module('myApp').controller('addItemModalInstanceCtrl', ['$scope', '$uibModalInstance', 'modalItem', '$http', 'myConstant'
    , function ($scope, $uibModalInstance, modalItem, $http, myConstant) {
        var vm = $scope.vm = {
            modalTitle: 'please change the title',
            modalBody: '#',
            url: '',
            item: {}
        };
        vm = $scope.vm = modalItem;
        vm.constant = myConstant;

        $scope.save = function () {
            var postBean = angular.copy(vm.item);
            postBean.orderAmt = Big(postBean.orderAmt).times(100).toString();

            $http.post(vm.url, postBean, config.jsonHeader)
                .then(function (response) {
                    if (response.data.success) {
                        alert("操作成功");
                        $scope.cancel();
                    } else {
                        alert("操作失败");
                    }
                }, function (response) {
                    alert("操作失败");
                })
        }

        $scope.cancel = function () {
            $uibModalInstance.dismiss('cancel');
        }
    }]);